package com.boot.dog.common.auth;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.boot.dog.common.constant.SecurityConst;
import com.boot.dog.entities.pojo.Permission;
import com.boot.dog.entities.pojo.Role;
import com.boot.dog.entities.pojo.User;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: MaxLv
 * @Date: 2019/12/2
 */
@Data
public class DogUser implements UserDetails {

    private static final long serialVersionUID = 1L;
    private String username;
    private String password;
    private String usertype;
    private Integer status;
    private List<Role> roles;
    private List<Permission> permissions;
    private List<GrantedAuthority> authorities;

    public DogUser(User user) {
        this.setUsername(user.getUsername());
        this.setPassword(user.getPassword());
        this.setUsertype(user.getUserType());
        this.setStatus(user.getStatus());
        this.setRoles(user.getRoles());
        this.setPermissions(user.getPermissions());
    }

    public DogUser(String username) {
        this.username = username;
    }

    public DogUser(String username, String password, String usertype, List<GrantedAuthority> authorities) {
        this.username = username;
        this.password = password;
        this.usertype = usertype;
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorityList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(this.getPermissions())) {
            authorityList.addAll(this.getPermissions().stream().filter(permission -> SecurityConst.PERMISSION_OPERATION.equals(permission.getType())
                    && StrUtil.isNotBlank(permission.getTitle())
                    && StrUtil.isNotBlank(permission.getPath())).map(permission -> new SimpleGrantedAuthority(permission.getTitle())).collect(Collectors.toList()));
        }
        if (CollectionUtil.isNotEmpty(this.getRoles())) {
            authorityList.addAll(this.getRoles().stream().filter(role -> StrUtil.isNotBlank(role.getName())).map(role -> new SimpleGrantedAuthority(role.getName())).collect(Collectors.toList()));
        }
        return authorityList;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return !SecurityConst.USER_STATUS_LOCK.equals(this.getStatus());
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return SecurityConst.USER_STATUS_NORMAL.equals(this.getStatus());
    }
}
